<template>
  <div class="login-container">
    <!-- 顶部 -->
    <van-nav-bar title="登录" left-text="返回" left-arrow @click-left="$router.go(-1)"/>
    <!-- 输入框 -->
    <van-form @submit="onSubmit">
      <van-field
        v-model="mobile"
        name="mobile"
        label="手机号"
        placeholder="请输入手机号"
        required
        :rules="[
          { required: true, message: '请填写手机号' },
          {
            pattern:
              /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/,
            message: '请输入正确手机号',
          },
        ]"
      />
      <van-field
        v-model="code"
        name="code"
        label="验证码"
        placeholder="请输入验证码"
        required
        :rules="[
          { required: true, message: '请填写验证码' },
          { pattern: /^\d{6}$/, message: '请输入正确验证码' },
        ]"
      />
      <div style="margin: 16px">
        <van-button round block type="info" native-type="submit"
          >登录</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
// 引入登录接口
import { userLogin } from "./user.js";

// 导入token
import { setToken } from "@/utils/token.js";

export default {
  name: "login",
  data() {
    return {
      mobile: "",
      code: "246810",
    };
  },
  methods: {
    onSubmit(values) {
      userLogin(values).then((res) => {
        // 存储token
        setToken(res.data.data);
        // 轻提示框
        this.$toast.success("登录成功");
        // 跳转到首页
        this.$router.push('/home');
      })
      .catch(err => {
        this.$toast.fail("登录失败！");
      })
    },
  },
};
</script>

<style lang="less">
.login-container {
  .van-nav-bar__content {
    background-color: #0098fa;
    .van-nav-bar__title,
    .van-icon,
    .van-nav-bar__text {
      color: #fff;
    }
  }
}
</style>